import 'dart:io';

import 'package:android_intent_plus/android_intent.dart';
import 'package:flutter/material.dart';
import 'package:flutter_blue_plus/flutter_blue_plus.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

import 'package:get/get.dart';

import '../../../routes/app_pages.dart';
import '../../../utils/NetWorkUtil.dart';
import '../../../utils/PermissionUtil.dart';
import '../../../widget/tips.dart';
import '../controllers/permission_setting_controller.dart';

class PermissionSettingView extends GetView<PermissionSettingController> {
  const PermissionSettingView({super.key});
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        leading: IconButton(
          icon: Image.asset('assets/images/ic_back2.png',width: 48.w,height: 48.h),
          onPressed: () {
            Get.back();
          },
          tooltip: MaterialLocalizations.of(context).backButtonTooltip, // 可选：设置工具提示
        ),

      ),
      body: ListView(
        children: [
          Obx(()=>Container(
            padding: EdgeInsets.all(10.w),
            child: Column(
              children: [
                SizedBox(height: 200.h),
                Image.asset('assets/images/ic_permission_checking.webp',width: 540.w,height: 350.h),
                SizedBox(height: 16.h),
                Text('蓝牙与位置信息',style: TextStyle(fontSize: 36.sp),),
                SizedBox(height: 10.h),
                Container(
                  margin: EdgeInsets.only(left: 94.w,right: 94.h),
                  child: Text('请打开蓝牙,并允许应用获取您的位置信息,以用于连接机器人.',style: TextStyle(fontSize: 28.sp),),
                ),

                SizedBox(height: 408.h),
                Container(
                    width: 594.w,
                    child: ElevatedButton(
                        onPressed: controller.blueteethHasOpen.value ? null : () async {
                          if (Platform.isAndroid) {
                            try {
                              await FlutterBluePlus.turnOn();
                              controller.changeBlueTeethBtn(true);
                            } catch(e) {
                              controller.changeBlueTeethBtn(false);
                              e.printInfo();
                            }


                          } else {
                            Tips.showToast('请手动打开.');
                          }
                        }, child: Row(
                               children: [
                               SizedBox(width: 130.w,),
                               controller.blueteethHasOpen.value ? Image.asset('assets/images/ic_confirmed.png',width: 50.w,height: 50.h) : SizedBox(),
                               SizedBox(width: 37.w),
                                 controller.blueteethHasOpen.value ? Text('蓝牙已打开'): Text('打开蓝牙')
                    ],))
                ),
                SizedBox(height: 10,),
                GetPlatform.isAndroid ?
                Container(
                    width: 594.w,
                    child: ElevatedButton(onPressed: controller.locationHashOpen.value ? null : () async {
                      // 打开系统设置页面
                      if (GetPlatform.isAndroid) {
                        await AndroidIntent(
                        action: 'android.settings.LOCATION_SOURCE_SETTINGS').launch();

                        print('位置信息更新...');
                      }
                    }, child: Row(
                      children: [
                        SizedBox(width: 130.w,),
                        controller.locationHashOpen.value ? Image.asset('assets/images/ic_confirmed.png',width: 50.w,height: 50.h) : SizedBox(),
                        SizedBox(width: 37.w),
                        controller.locationHashOpen.value ? Text('已授权位置信息'): Text('授权位置信息')
                      ],),
                    ))
                : SizedBox(),
                /*SizedBox(height: 10,),
                GestureDetector(
                    onTap: () async {
                      PermissionUtil.requestPermission();
                      // 绑定前检查,网络, 蓝牙和位置信息.
                      bool isNetWork = await NetWorkUtil.checkNetWorkIsAvailable();
                      if (!isNetWork) {
                        Tips.showToast('网络不可用.');
                        return;
                      }

                      bool locationTurnOn = await checkLocationDisable();
                      bool blueethTurnOn = await checkBlueteethDisable();
                      if (locationTurnOn || blueethTurnOn) {
                        return;
                      }

                      bool permissionAllGranted = await PermissionUtil.checkPermission();
                      if (!permissionAllGranted) {
                        Get.toNamed(Routes.PERMISSION_SETTING);
                        return;
                      }

                      Get.toNamed(Routes.DEVICE_CONNECTION);
                    },
                    child: Text('完成授权',style: TextStyle(color: Colors.blue))
                )*/
              ],
            ),
          ))
        ],
      ),
    );
  }
}
